package net.huashitong.ssydt.provider.comment.service;

import net.huashitong.comment.ApiCommentManager;
import net.huashitong.comment.http.Result;
import net.huashitong.comment.vo.BaseCommentQueryVO;
import net.huashitong.comment.vo.CommentInVo;
import net.huashitong.comment.vo.CommentQueryVO;
import net.huashitong.comment.vo.HstComment;
import net.huashitong.comment.vo.NotCheckPassVO;
import net.huashitong.comment.vo.UserCommonQueryVO;
import net.huashitong.ssydt.exceptions.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author <a href="mailto:yhy23456@163.com">huiyang.yu</a>
 * @since 2017.09.20
 */
@Service
public class SdkCommentServiceImpl implements SdkCommentService {

    private ApiCommentManager apiCommentManager;

    @Autowired
    public SdkCommentServiceImpl(ApiCommentManager apiCommentManager) {
        this.apiCommentManager = apiCommentManager;
    }

    @Override
    public List<HstComment> getHotCommonList(CommentQueryVO commonQueryVO) {
        Result<ArrayList<HstComment>> result = apiCommentManager.getHotCommonList(commonQueryVO);
        return getArrayResults(result);

    }

    @Override
    public List<HstComment> getUserCommonList(UserCommonQueryVO userCommonQueryVO) {
        Result<ArrayList<HstComment>> result = apiCommentManager.getUserCommonList(userCommonQueryVO);
        return getArrayResults(result);
    }

    @Override
    public List<HstComment> getNewestCommonList(CommentQueryVO commonQueryVO) {
        Result<ArrayList<HstComment>> result = apiCommentManager.getNewestCommonList(commonQueryVO);
        return getArrayResults(result);
    }

    @Override
    public HstComment getComments(Long commentId) {
        Result<HstComment> result = apiCommentManager.getComments(commentId);
        return getResults(result);
    }

    @Override
    public void postComments(CommentInVo commentInVo) {
        apiCommentManager.postComments(commentInVo);
    }

    @Override
    public void deleteComments(Long commentId) {
        apiCommentManager.deleteComments(commentId);
    }

    @Override
    public Boolean getCommentUsersPraise(Long commentId, String userId) {
        Result<Boolean> result = apiCommentManager.getCommentUsersPraise(commentId, userId);
        return getResults(result);
    }

    @Override
    public void postCommentUsersPraise(Long commentId, String userId) {
        apiCommentManager.postCommentUsersPraise(commentId, userId);
    }

    @Override
    public void deleteCommentUsersPraise(Long commentId, String userId) {
        apiCommentManager.deleteCommentUsersPraise(commentId, userId);
    }

    @Override
    public Integer getNonCheckPass(NotCheckPassVO commonQueryVO) {
        Result<Integer> result = apiCommentManager.getNotCheckPass(commonQueryVO);
        return getResults(result);
    }

    @Override
    public Integer getCommentsNumber(BaseCommentQueryVO baseCommentQueryVO) {
        Result<Integer> result = apiCommentManager.getCommentsNumber(baseCommentQueryVO);
        return getResults(result);
    }


    private <T extends Serializable> List<T> getArrayResults(Result<ArrayList<T>> result) {
        if (result.getCode().equals(Result.Status.SUCCESS)) {
            return result.getResults();
        } else {
            throw new ServiceException(result.getCode(), result.getMessage());
        }
    }

    private <T extends Serializable> T getResults(Result<T> result) {
        if (result.getCode().equals(Result.Status.SUCCESS)) {
            return result.getResults();
        } else {
            throw new ServiceException(result.getCode(), result.getMessage());
        }
    }
}
